package com.hotel.config.rabbitmq.customer;

import com.alibaba.fastjson.JSONObject;
import com.hotel.customersystem.pojo.OrderVo;
import com.hotel.customersystem.service.CustomerOrderService;
import com.rabbitmq.client.Channel;
import org.aspectj.weaver.ast.Or;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.hotel.config.rabbitmq.config.CutTopConfig.CUTTOP_ORDER_QUEUE;

@Component
public class CutTopConsumer {
    @Autowired
    private CustomerOrderService customerOrderService;

    @RabbitHandler
    @RabbitListener(queues = CUTTOP_ORDER_QUEUE)
    public String saveOrder(String JSONStr, Message message, Channel channel){
        OrderVo orderVo = JSONObject.parseObject(JSONStr , OrderVo.class);
        String orderNo = customerOrderService.saveOrder(orderVo.getOrder(), orderVo.getRoomInfo(), orderVo.getTenantList());
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("**************************************\n"+orderNo);
        return orderNo;
    }
}
